

//富文本编辑器
function rich_text() {
    var editor = new UE.ui.Editor({ initialFrameWidth: $('form').width(), initialFrameHeight:300});
    editor.render('ueditor');
}

//上传图片的参数配置
function upload_img_config(url) {
    var  ratio = window.devicePixelRatio || 1,    //不同设备下的参数配置
        $container_img = $('#datum_img'),                //放置缩略图、预览的地方
        pick ='#datum_btn_img',
        widthThumb   = 110* ratio,           //缩略图的宽度
        heightThumb  = 110 * ratio,         //缩略图的高度
        maxFileSize      = 2,                 //上传单文件最大 8 M
        img ='gif,jpg,jpeg,bmp,png',
        upload_img;
        upload_img=WebUploader.create({
        auto: true,   //是否自动提交

        swf: '__PUBLIC__/home/js/webuploader/Uploader.swf',  //swf 文件

        server:url,   //服务器接收文件的地址

        pick:pick,//创建上传的按钮

       accept:{
            title: 'Images', //任意
            extensions: img,//图片允许的后缀
            mimeTypes: 'image/jpg,image/jpeg,image/png' //图片允许的类型
        },//上传文件

        duplicate :false,  //是否允许重复提交

        fileSizeLimit:maxFileSize*1024*1024, //允许上传文件的大小最大

        prepareNextFile:true,  //上传多个文件可以节约点时间

    });

      file_queued(upload_img ,$container_img,widthThumb,heightThumb);   //文件进来时候触发的函数

      file_progress(upload_img);                        //上传文件进度条

      //file_upload(upload_img);                         //手动上传文件

      file_success(upload_img,pick);                       //上传文件成功之后

      file_error(upload_img);                       // 上传文件错误的信息

      error(upload_img,maxFileSize,img);

      file_remove_view(upload_img,$container_img);
}

function upload_file_config(url) {
   var  ratio = window.devicePixelRatio || 1,
       $container_file = $('#datum_file'),
       pick ='#datum_btn_file',
       widthThumb   = 110* ratio,           //缩略图的宽度
       heightThumb  = 110 * ratio,         //缩略图的高度
       maxFileSize      = 8,                 //上传单文件最大 8 M
        file_type ='zip,rar',
       mimeTypes = '.zip,.rar',
       upload_file;

    upload_file =WebUploader.create({
        auto: true,
        swf: '__PUBLIC__/home/js/webuploader/Uploader.swf',
        server:url,   //服务器接收文件的地址
        pick:pick,//创建上传的按钮
        duplicate :false,  //是否允许重复提交
        fileSizeLimit:maxFileSize*1024*1024, //允许上传文件的大小最大
        accept :{
            title :'file',
            extensions:file_type,
            mimeTypes:mimeTypes
        },
        prepareNextFile:true,  //上传多个文件可以节约点时间
    });

    file_queued(upload_file, $container_file, widthThumb,heightThumb);
    file_progress(upload_file);                        //上传文件进度条
   // file_upload(upload_file);                         //手动上传文件
    file_success(upload_file,pick);                       //上传文件成功之后

    error(upload_file,maxFileSize,file_type);
    file_remove_view(upload_file,$container_file);
}



function file_queued(upload,$container,widthThumb,heightThumb){
    upload.on('fileQueued',function (file) {

            $container.append(img_html(file));   //向容器内添加图片
            upload.makeThumb(file, function (error, src) {
                var img = $('#'+file.id);
                if (error) {
                    img.parent().replaceWith(file_html(file));
                    return;
                }
                img.find('img').attr('src',src);
                $('.remove_upload').css({left:widthThumb,top:-heightThumb/2+10});
            }, widthThumb, heightThumb);  //缩略图


       var file_id  =$('#'+file.id).parent().prev().children().off().attr('id');

       console.log(file_id);
          if(file_id){
              upload.removeFile(file_id); //图片移出队列
              $('#'+file_id).parent().empty();  //删除容器内的图片
          }

        $('.datum_progress').css('width',widthThumb);

        $('.datum_name').css({width:widthThumb,textAlign:'center'});
    });



}

 function img_html(file) {
     var  html = '<li><div id="' + file.id + '" class="file-upload text-left">';
          html += '<span class="glyphicon glyphicon-remove remove_upload"></span>';
          html += '<img/>',
          html += '<div class="datum_name">' + file.name + '</div>';
          html +='<div class="datum_progress"><span class="text">等待上传</span><span class="datum_percentage"></span></div>';
          html += '</div></li>';

    return html;
 }


function file_html(file) {
    var html = '<li><div id="'+ file.id +'" class="file-upload text-left">';
        html +='<span class="glyphicon glyphicon-remove remove_upload"></span>';
        html +='<div class="">'+file.name+'</div>';
        html +='<div class="datum_progress"><span class="text">等待上传</span><span class="datum_percentage"></span></div>';
        html +='</div></li>';

        return html;
}


function file_progress(upload) {

    upload.on('uploadProgress',function (file ,percentage) {

        var percent = $('#'+file.id).find('.datum_progress span');

        percent.css('width',percentage * 100+ '%');

        $('#'+file.id).find('span.text').text(percentage * 100 +'%');

    });
}


/**
 * response 是服务返回的数据
 */
//上传文件成功
function file_success(upload,name) {
    upload.on('uploadSuccess',function (file,response) {

            if(typeof (response) =='object'){
                $(name).next(':hidden').val(response.url);
            }else {
                layer.open({
                    content:response,
                });

               $('#'+file.id).find('span.text').text('文件上传失败,,请重新选择文件').css('color','#ff0000');

            }
    });
}

function file_error(upload) {

    upload.on('uploadError',function (file) {
        var $li = $( '#'+file.id ),
            $error = $li.find('div.error');
        // 避免重复创建
     /*   if ( !$error.length ) {
            $error = $('<div class="error"></div>').appendTo( $li );
        }*/

        $error.text('上传失败');
    });
}


function file_remove_view(upload,$container) {

    $container.on('click','.remove_upload',function () {

        var file_id = $(this).parent().attr('id');

       upload.removeFile(file_id,true);

        $(this).off().parent().remove();
    });

}

function  file_upload(upload) {

    $('#upload_file').click(function () {
     upload.upload();

        $('body,html').animate({scrollTop:0},500);


        $.ajax({


        });



        layer.open({
            content : '文件上传中请勿刷新页面',
            time :3000,
        });

    });
}

function error(upload,max_file_size,file_type) {

    upload.on('error',function (type) {

        if(type == 'Q_TYPE_DENIED'){
            layer.open({
                content: file_type,
            });
        }else if(type =='Q_EXCEED_SIZE_LIMIT'){
            layer.open({
                content :'只能上传'+max_file_size +'M以内的文件、图片',
            });
        }else if(type =='F_DUPLICATE'){
            layer.open({
                content :'文件以被选择请勿在选',
            });
        }else{
            layer.open({
                content :'文件错误！！请重新检查文件！！错误代码'+type,
            });

        }
    });
}

function del_ajax() {

    $.ajax({



    });
}